package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    //图片
    @Autowired
    private GraphInfoService graphInfoService;
    //配套信息
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    //杂费信息
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    //标签信息
    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;


    @Override
    public void customizeSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        boolean b = apartmentSubmitVo.getId() != null;
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        saveOrUpdate(apartmentSubmitVo);
        if(b){
            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =
                    new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, apartmentSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除配套
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper =
                    new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);
            //删除标签
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper =
                    new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
            //删除杂费
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper =
                    new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        }
        //插入图片
        //获取一个GraphVo对象的列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //检查列表是否为空，不为空执行后续代码
        if (!CollectionUtils.isEmpty(graphVoList)){
            //创建一个新的 arraylist 用来存储 GrapInfo 对象
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            //开始遍历 GraphInfo 中每一个 GraphVo 对象
            for (GraphVo graphInfo : graphVoList) {
                //为每个 GraphVo 对象创建一个新的 GraphInfo 对象
                GraphInfo info = new GraphInfo();
                info.setItemId(apartmentSubmitVo.getId());
                info.setItemType(ItemType.APARTMENT);
                info.setName(graphInfo.getName());
                info.setUrl(graphInfo.getUrl());
                //将新的 GraphInfo 对象添加到 graphInfos 列表中
                graphInfos.add(info);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //插入配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> facilityInfos = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                facilityInfos.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityInfos);
        }
        //插入标签
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> feeValues  = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                feeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeValues);
        }
        //插入杂费
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> labels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                labels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labels);
        }
    }

    @Override
    public void pageApartmentItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.pageApartmentItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo==null) {return null;}

        //查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        //查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListById(id);
        //查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListById(id);
        //查询杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListById(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //同属性赋值方法
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        //查询公寓对应的房间 有房间返回异常
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count > 0){
            //自定义异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //直接删除公寓
        removeById(id);
        //删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //删除标签信息
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //删除配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

    }


}




